﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using Requests = ServerSide.Requests;
using System.Reflection;
using ServerSide.Requests;
using ServerSide.Common.Types;
using ServerSide.Common;

namespace ClientSide.Common.Helpers
{
    public class RequestSender
    {
        public const string GUEST_USER_NAME = "GUEST";
        public const string GUEST_USER_PASSWORD = "123456";

        public static RequestMessage RequestMessage { get; set; }

        // bu yanlızca bir kez çalıştırılacak constructor
        static RequestSender()
        {
            InitializeRequestMessage(GUEST_USER_NAME, GUEST_USER_PASSWORD);
        }

        public static void InitializeRequestMessage(string UserName, string Password)
        {
            RequestMessage = new RequestMessage();
            RequestMessage.UserName = UserName;
            RequestMessage.Password = Password;
        }

        /// <summary>
        /// Request i göndermek için kullanılır.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="requestResulType"></param>
        /// <param name="responseAction">Request çalıştırılıp result döndüğünde çalışacak metod</param>
        /// <param name="errorAction">Result oluşturulurken hata alındığı durumda çalışacak metod</param>
        public static void SendRequest(object request, Type requestResulType, EventHandler responseAction, EventHandler errorAction)
        {

            //RequestMessage oluşturuluyor
            RequestMessage.RequestTypeName = request.GetType().ToString();
            RequestMessage.SerializedRequest = Helpers.Serializer.ObjectToString(request);

            // Requesti ServerSide'a gönderiyoruz
            ServerSide.RIA.Web.EOrganizatorDomainContext dc = new ServerSide.RIA.Web.EOrganizatorDomainContext();
            InvokeOperation<ResponseMessage> response = dc.DoWork(RequestMessage);

            // Dönen cevap işleniyor
            response.Completed += new EventHandler((s, e) =>
            {
                if (!response.HasError)
                {
                    ResponseMessage responseMessage = (ResponseMessage)response.Value;

                    // Requeste ait result alınıyor
                    object requestResult = Serializer.StringToObject(responseMessage.SerializedRequestResult, requestResulType);

                    // Response tipinr göre aksiyon belirleniyor
                    switch (responseMessage.ResponseType)
                    {
                        case ResponseType.Succes:

                            if (requestResult.GetType().GetProperty("ErrorMessage").GetValue(requestResult, null) == null)
                            {
                                // Hata yoksa
                                responseAction(requestResult, e);
                            }
                            else
                            {
                                // hata varsa
                                errorAction(requestResult, e);
                            }
                            break;
                        case ResponseType.ErrorOccured:
                            MessageBox.Show(responseMessage.Message);
                            break;
                        case ResponseType.NeedLogin:
                            MessageBox.Show("Lütfen giriş yapınız");
                            break;
                        default:
                            MessageBox.Show("BaseResponse.ResponseType set edilmedi");
                            break;
                    }

                }
                else
                {
                    if (errorAction != null)
                        errorAction(null, null);
                    else
                        MessageBox.Show("Hata!\n" + response.Error.Message);
                }
            }

            );
        }

    }
}
